package com.zxh.listdemo.adapter.list;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zxh.listdemo.ListActivity;
import com.zxh.listdemo.R;
import com.zxh.listdemo.model.OneModel;

import java.util.List;

/**
 * 单一布局的适配器的基本使用
 * Created by zhangxuehui on 2018/2/27.
 */
public class OneAdapter extends BaseAdapter {

    private List<OneModel> mList;
    private Context mContext;
    private LayoutInflater mInflater;

    public OneAdapter(Context context, List<OneModel> list) {
        this.mContext = context;
        this.mList = list;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public OneModel getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.item_one, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        mHolder.mTvTitle.setText(mList.get(position).getTitle());
        mHolder.mTvDesc.setText(mList.get(position).getDesc());
        Log.e(ListActivity.class.getSimpleName(), mList.get(position).getLink());
        Picasso.with(mContext).load(mList.get(position).getLink()).into(mHolder.mIvIcon);
        return convertView;
    }

    public static class ViewHolder {

        private TextView mTvDesc;
        private TextView mTvTitle;
        private ImageView mIvIcon;

        public ViewHolder(View view) {
            mIvIcon = view.findViewById(R.id.iv_icon);
            mTvTitle = view.findViewById(R.id.tv_title);
            mTvDesc = view.findViewById(R.id.tv_desc);
        }
    }
}
